import { ref } from "vue";
import { ElLoading } from "element-plus";

class LoadingManager {
  constructor() {
    this.loadingInstance = ref(null);
  }

  show(options = {}) {
    if (this.loadingInstance.value) {
      this.loadingInstance.value.close();
    }
    this.loadingInstance.value = ElLoading.service({
      // fullscreen: true,
      lock: true,
      text: "加载中...",
      background: "rgba(0, 0, 0, 0.0.1)",
      ...options,
    });
  }

  hide() {
    if (this.loadingInstance.value) {
      this.loadingInstance.value.close();
      this.loadingInstance.value = null;
    }
  }
}

const loading = new LoadingManager();
export default loading;

export const FYLoadingPlugin = {
  install(app) {
    app.config.globalProperties.$loading = new LoadingManager();
  },
};

